﻿using System;
using System.Collections.Generic;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Biz
{
    public class DonViBiz
    {
        #region Properties

        private IDonViDao _donViDao;
        private IDonViDao DonViData
        {
            get { return _donViDao ?? (_donViDao = new DonViDao()); }
        }

        #endregion

        #region Constructors

        #endregion

        #region Methods

        public List<DonVi> GetAllDonVis()
        {
            return DonViData.GetAll();
        }

        public object GetDonVis(int startIndex, int pageSize, string sorting, string name)
        {
            try
            {
                int totalRecords;
                var donVis = DonViData.GetDonVis(startIndex, pageSize, sorting, name, out totalRecords);
                return new { Result = "OK", Records = donVis, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public DonVi CreateDonVi(DonVi donVi)
        {
            // validate inputs
            if (donVi == null) return null;

            return string.IsNullOrEmpty(donVi.Ten) ? null : DonViData.AddDonVi(donVi);
        }

        public DonVi GetById(int id)
        {
            return id > 0 ? DonViData.GetById(id) : null;
        }

        public void UpdateDonVi(DonVi donVi)
        {
            if (donVi == null) return;

            DonViData.UpdateDonVi(donVi);
        }

        public object DeleteDonVi(int id)
        {
            try
            {
                DonViData.DeleteDonVi(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion
    }
}
